package org.xtframe.sql.verify;

import java.util.List;
import java.util.Map;

import org.xtframe.common.EncryptCount;
import org.xtframe.entity.UserData;
import org.xtframe.sql.SqlDataVerify.VerifyType;
import org.xtframe.sql.PreparedParameter;
import org.xtframe.sql.VerifyBase;
import org.xtframe.util.CommonUtil;
import org.xtframe.util.ContextUtil;
import org.xtframe.util.StringUtil;

public class ReplaceVerify implements VerifyBase {
	
	// Sql替换参数（替换、检查）
	public enum ReplaceParameter {
		USERNAME, USERID, ORGANID, ATORGANID, UUID, CURRENTTIME, MD5
	}
	
	private String position = "";
	
	private ReplaceParameter value = null;
	
	public void init(Map<String, Object> data){
		if(data.containsKey("position")) this.position = data.get("position").toString();
		if(data.containsKey("value")) this.value = ReplaceParameter.valueOf(data.get("value").toString().toUpperCase());
	}

	public String getPosition() {
		return this.position;
	}

	public VerifyType getXtype() {
		return VerifyType.replace;
	}

	public boolean check(Map<String, Object> parameters) {
		return false;
	}

	public String replace(Map<String, Object> parameters, List<PreparedParameter> preparedParameters) {
		return getReplace(value, StringUtil.toString(parameters.get(position), ""));
	}
	
	/**
	 * 获取替换数据
	 * 
	 * @param parameter
	 * @param value
	 * @param userData
	 * @return
	 */
	public static String getReplace(ReplaceParameter parameter, String value) {
		UserData userData = ContextUtil.getCurrentUserData();
		if (parameter == ReplaceParameter.USERNAME) {
			return userData.getUser().getUserName();
		} else if (parameter == ReplaceParameter.USERID) {
			return userData.getUser().getUserId();
		} else if (parameter == ReplaceParameter.ORGANID) {
			return "" + userData.getUser().getOrganId();
		} else if (parameter == ReplaceParameter.ATORGANID) {
			return userData.getOrgan().getAtOrganId();
		} else if (parameter == ReplaceParameter.UUID) {
			return CommonUtil.getUUID();
		} else if (parameter == ReplaceParameter.CURRENTTIME) {
			return CommonUtil.getCurrentTime();
		} else if (parameter == ReplaceParameter.MD5) {
			return EncryptCount.encryptMD5(value);
		} else {
			return value;
		}
	}

}
